package com.sznaner.tool.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Stack;

public class FileScannerUtil {

    //扫描所有文件
    public static ArrayList<String> scanFiles(String path) {
        return scanFiles(path,new ArrayList<String>(),null);
    }


    /**
     *扫描文件
     *@params path 路径
     *@params path exceptDirs 排除的字文件夹数组
     *@params path types 要扫描哪些格式的文件例如:.mp3 .png
     *@return
     */
    public static ArrayList<String> scanFiles(String path, List<String> exceptDirs,List<String>types) {

        ArrayList<String> mFileList = new ArrayList<>();

        if(path == null) return mFileList;

        if(!MyPathUtil.checkPath(path)) return mFileList;

        mFileList.clear();
        File file = new File(path);
        Stack<File> folderStack = new Stack<>();
        folderStack.push(file);

        long starttime = System.currentTimeMillis();

        while (!folderStack.empty()) {

            file = folderStack.pop();
            File[] files = file.listFiles();

            //　内部排序
            Arrays.sort(files, new Comparator<File>() {
                @Override
                public int compare(File o1, File o2) {
                    return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
                }
            });

            for (int i = 0; i < files.length; i++) {
                String fullfilepath = files[i].getAbsolutePath();

                if(exceptDirs.size()>0 && files[i].isDirectory()){
                    if(exceptDirs.contains(files[i].getName())){
                        continue;
                    }
                }

                if (files[i].isDirectory() && !"./".equals(files[i].getName())) {
                    folderStack.push(files[i]);
                    String ext = MyPathUtil.getExt(fullfilepath);
                    if(types.size() > 0){
                        if(ext !=null && types.contains(ext)){
                            mFileList.add(fullfilepath);
                        }
                    }else {
                        mFileList.add(fullfilepath);
                    }
                } else {
                    String ext = MyPathUtil.getExt(fullfilepath);
                    if(types.size() > 0){
                        if(ext !=null && types.contains(ext)){
                            mFileList.add(fullfilepath);
                        }
                    }else {
                        mFileList.add(fullfilepath);
                    }
                }
            }
        }

        long timeuse = System.currentTimeMillis() - starttime;

        Arrays.sort(mFileList.toArray());

        //　外部排序
        mFileList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });


        ArrayList<String> resultFileList = new ArrayList<>();
        //排除.开头文件
        for (String tempPath : mFileList){
            File tempFile = new File(tempPath);
            String fileName = MyPathUtil.getName(tempFile);
            if(fileName.length()>0 && !fileName.substring(0,1).equals(".")){
                resultFileList.add(tempPath);
            }
        }
        return resultFileList;
    }

//    //扫描路径获取图片
//    public static  List<ScreenSaverImageModel> scanImagesWithPath(String path){
//        List<ScreenSaverImageModel> result = new ArrayList<>();
//
//        //排除文件夹
//        List<String> exceptDir = new ArrayList<>();
//        exceptDir.add("Android");
//
//        //扫描哪些格式
//        List<String> types = new ArrayList<>();
//        types.add(".jpg");
//        types.add(".png");
//
//        List<String> allFiles = FileScannerUtil.scanFiles(path,exceptDir,types);
//        for (String fpath :allFiles){
////            if(fpath.contains(".jpg") || fpath.contains(".png")){
//                result.add(new ScreenSaverImageModel(fpath,FileHandleUtil.getNameAndExt(fpath)));
////            }
//        }
//        return result;
//    }

}